import java.util.Iterator;
import java.util.Set;


/* 
 * Sourabh Jain; sourj@cs.umn.edu
 * World for the viro simulations
 */

public class Bucket{
	public int bucket_level=-1;
	public long gw=-1;
	/*
	 * It stores the gateway vid if there is some, else it is null
	 */
	public long dest=-1;
	/*
	 * It stores the destination node's vid
	 */
	public Prefix prefix;
	/*
	 * It stores the prefix for the bucket
	 */
	public long nexthop=-1;
	/*
	 * It stores the next hop node's vid
	 */
	public Bucket(){
	}
	public Bucket (Bucket b){
		this.bucket_level = b.bucket_level;
		this.gw = b.gw;
		this.dest = b.dest;
		this.prefix = b.prefix;
		this.nexthop = b.nexthop;
	}
	public void copyFrom(Bucket b){
		this.bucket_level = b.bucket_level;
		this.gw = b.gw;
		this.dest = b.dest;
		this.prefix = b.prefix;
		this.nexthop = b.nexthop;
	}
	public String toString(){
		String str = "";
		str = "Bucket_level: " + bucket_level;
		str = str+ "\tprefix: " + getPrefixString();
		str = str + "\tnexthop: " +  World.toBinaryString(nexthop)+ "("+nexthop+")" ;
		str = str + "\tgw: " + World.toBinaryString(gw)+ "("+gw+")";	
		
		return str;
	}
	
	public String getPrefixString() {
		return prefix+"";
	}
	
	public boolean equals(Object b1){
		
		//System.out.println("\n\n===========>  Bucket Comparison was called\n\n");
		boolean ans = true;
		Bucket b2 = (Bucket) b1;
		if (b2.bucket_level == this.bucket_level && b2.dest == this.dest && b2.nexthop == this.nexthop ){
			// now compare the prefix sets
	
			if (! (this.prefix.len == b2.prefix.len &&  this.prefix.maxlen == b2.prefix.maxlen && this.prefix.prefix == this.prefix.prefix) ){
				ans = false;
			}
			/*
			 * Instead of using Set class's contains method, we should use equals method of 
			 * prefix class. Becuase bucket's prefix is no more a set
			else if ( b2.prefix.equals(this.prefix)){
				ans = false;					
			}
			*/
		}
		else{
			ans = false;
		}
		//System.out.println("Bucket comparison: b1 = "+(Bucket)b1+"\n b2 = " + this+ " ANS: "+ans);
		return ans;
	}
	
	public int hashCode(){
		return (this.dest+this.nexthop+this.bucket_level+"").hashCode();
	}
}
